<?php

class attention_controller_index extends SnsController {
	/**
	 * @var attention_model_data
	 */
	var $o;
	/**
	 * 每页显示个数
	 *
	 */
	const PAGESIZE=20;
	
	public function __construct($app, $controller){
		parent::__construct($app, $controller);
		$this->o = new attention_model_data();
		
	}
	
	/**
	 * 添加
	 *
	 */
	public function add_action(){
		$this->_renderJson();
		$this->requireLogin();
		$uid = _('uid');
		$username = member_util_login::getMemberInfo($uid,'username');
		if(!$username or !$uid){
			$this->ret = self::failure('您关注的用户不存在');
		}else{
			if($uid == self::$login['uid']){
				$this->ret = self::failure('您不能关注您自己');
			}else{
				if($this->o->chkAttention(self::$login['uid'], $uid)){
					$this->ret = self::failure('该用户已经是你的关注对象' );
				}else{
					$flag = $this->o->save(array(
					'uid'		=> $uid,
					'username'	=> $username,
					'host_id'	=> self::$login['uid']
					));
					if($flag){
						$this->cleanCache(self::$login['uid']);
						if($this->o->has($uid, self::$login['uid'])){
							//								$flag = Model::factory('')
						}else{
							$this->ret = self::success(array(
							'uid'=>$uid, 'username'=>$username, 'add'=>true));
							$this->addMessage('您已将他加为关注对象');
						}
					}else{
						$this->ret = self::failure('位置错误' );
					}
				}
			}
		}
	}
	
	public function add_attention_action(){
		$this->_renderJson();
		$this->requireLogin();
		$uid = _('uid');
		$username = member_util_login::getMemberInfo($uid,'username');
		if(!$username or !$uid){
			$this->ret = self::failure('您关注的用户不存在');
		}else{
			if($uid == self::$login['uid']){
				$this->ret = self::failure('您不能关注您自己');
			}else{
				if($this->o->chkAttention(self::$login['uid'], $uid)){
					$this->ret = self::failure('该用户已经是你的关注对象' );
				}else{
					$flag = $this->o->add_attention(self::$login['uid'],$uid);
					if($flag){
						$this->ret = self::success();
					}else{
						$this->ret = self::failure('未知错误' );
					}
				}
			}
		}
	}
	
	
	/**
	 * 获得分页列表
	 *
	 */
	public function att_list_action(){
		$uid = $this->getUid();
		$page = _('page',1);
		if($uid){
			$list = $this->o->getAttList($uid,$page,15);
			$pages = $this->genPages($list['INFO']);
			$this->ret = array('data'=>$list['ROWS'],'pages'=>$pages);
//			$this->ret = self::success(Cache::cacheFunction(array($this->o, 'selectByPage'), array($uid, $this->page?intval($this->page):1, self::PAGESIZE ), false));
		}else{
			$this->requireLogin();
		}
	}
	/**
	 * 首页嵌入
	 *
	 */
	public function get_embed_action(){
		$uid = $this->getUid();
		if($uid){
			$this->ret = Cache::cacheFunction(array($this->o, 'getList'), array($uid, 9));
			$this->view = $this->genAppViewPath('part_embed');
			$this->renderType = self::RENDER_TYPE_CUSTOM ;
		}else{
			$this->view = null;
		}
	}
	/**
	 * 删除
	 *
	 */
	public function remove_action(){
		$this->_renderJson();
		if(self::$login){
			$uid = self::$login['uid'];
			$att_uid = _('uid');
			/*if($userinfo = friends_util_cache::getUserInfo($uid)){
				$this->ret = array('result'=>false, 'reason'=>'您关注的用户不存在' );
			}*/
			if($att_uid == self::$login['uid']){
				$this->ret = array('result'=>false, 'reason'=>'你不能添加或取消对自己关注！' );
			}else{
				$flag = $this->o->remove_att($uid,$att_uid);
				if($flag === true){
					$this->ret = array('result'=>true);
					$this->addMessage('您已取消对该用户的关注');
				}else{
					$this->ret = array('result'=>false, 'reason'=>'未知错误' );
				}
			}
		}else{
			$this->ret = array('result'=>false, 'reason'=>'您没有登录，做本次操作' );
		}
/*		$this->view = $this->genRootViewPath('_json');
		$this->renderType = self::RENDER_TYPE_CUSTOM ;*/
	}
	/**
	 * 清除缓存
	 *
	 * @param int $uid
	 */
	public function cleanCache($uid){
		Cache::cleanFunctionCache(array($this->o, 'getList'), array($uid, 9));
		Cache::cleanFunctionCache(array($this->o, 'selectByPage'), array($uid, 1, self::PAGESIZE ));
		Cache::cleanFunctionCache(array($this->o, 'selectByPage'), array($uid, 2, self::PAGESIZE ));
		Cache::cleanFunctionCache(array($this->o, 'selectByPage'), array($uid, 3, self::PAGESIZE ));
	}
}